<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">
    <div class="article-intro">

        <h1>Java Applet 基础
        </h1><p>Applet 是一种 Java 程序。它一般运行在支持 Java 的 Web 浏览器内。因为它有完整的 Java API支持,所以Applet 是一个全功能的 Java 应用程序。</p><p>
        如下所示是独立的 Java 应用程序和 applet 程序之间重要的不同：</p>
        <ul>
            <li>
                Java 中 Applet 类继承了 java.applet.Applet 类。</li>
            <li>
                Applet 类没有定义 main()，所以一个 Applet 程序不会调用 main() 方法。</li>
            <li>
                Applet 被设计为嵌入在一个 HTML 页面。</li>
            <li>
                当用户浏览包含 Applet 的 HTML 页面，Applet 的代码就被下载到用户的机器上。</li>
            <li>
                要查看一个 Applet 需要 JVM。 JVM 可以是 Web 浏览器的一个插件，或一个独立的运行时环境。</li>
            <li>
                用户机器上的 JVM 创建一个 Applet 类的实例，并调用 Applet 生命周期过程中的各种方法。</li>
            <li>
                Applet 有 Web 浏览器强制执行的严格的安全规则，Applet 的安全机制被称为沙箱安全。</li>
            <li>
                Applet 需要的其他类可以用 Java 归档（JAR）文件的形式下载下来。</li>
        </ul>
        <hr>
        <h2>
            Applet的生命周期
        </h2>
        <p>
            Applet 类中的四个方法给你提供了一个框架，你可以再该框架上开发小程序：
        </p>
        <ul><li><strong>
            init: </strong>该方法的目的是为你的 Applet 提供所需的任何初始化。在 Applet 标记内的 param 标签被处理后调用该方法。</li><li><strong>
            start:</strong> 浏览器调用 init 方法后，该方法被自动调用。每当用户从其他页面返回到包含 Applet 的页面时，则调用该方法。</li><li><strong>
            stop:</strong> 当用户从包含 Applet 的页面移除的时候，该方法自动被调用。因此，可以在相同的 Applet 中反复调用该方法。</li><li><strong>
            destroy: </strong>此方法仅当浏览器正常关闭时调用。因为 Applet 只有在 HTML 网页上有效，所以你不应该在用户离开包含 Applet 的页面后遗漏任何资源。</li><li><strong>
            paint:</strong> 该方法在 start() 方法之后立即被调用，或者在 Applet 需要重绘在浏览器的时候调用。paint() 方法实际上继承于 java.awt。</li></ul>
        <hr><h2>"Hello, World" Applet:
    </h2>
        <p>
            下面是一个简单的 Applet 程序 HelloWorldApplet.java:
        </p>
        <div class="example">
            <h2 class="example">HelloWorldApplet.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">applet</span><span class="hl-code">.*;
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">awt</span><span class="hl-code">.*;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">HelloWorldApplet</span><span class="hl-code"> </span><span class="hl-reserved">extends</span><span class="hl-code"> </span><span class="hl-identifier">Applet</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">paint</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">Graphics</span><span class="hl-code"> </span><span class="hl-identifier">g</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">g</span><span class="hl-code">.</span><span class="hl-identifier">drawString</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Hello World</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-number">25</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>
            这些 import 语句将以下类导入到我们的 Applet 类中：
        </p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">applet</span><span class="hl-code">.</span><span class="hl-identifier">Applet</span><span class="hl-code">.
</span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">awt</span><span class="hl-code">.</span><span class="hl-identifier">Graphics</span><span class="hl-code">.</span></pre>
            </div>
        </div>
        <p>
            没有这些 import 语句，Java 编译器就识别不了 Applet 和 Graphics 类。
        </p>
        <hr>
        <h2>
            Applet 类</h2>
        <p>
            每一个 Applet 都是 java.applet.Applet 类的子类，基础的 Applet 类提供了供衍生类调用的方法,以此来得到浏览器上下文的信息和服务。
        </p><p>
        这些方法做了如下事情：</p>
        <ul><li>
            得到 Applet 的参数</li><li>
            得到包含 Applet 的 HTML 文件的网络位置</li><li>
            得到 Applet 类目录的网络位置</li><li>
            打印浏览器的状态信息</li><li>
            获取一张图片</li><li>
            获取一个音频片段</li><li>
            播放一个音频片段</li><li>
            调整此 Applet 的大小</li></ul>
        <p>除此之外，Applet 类还提供了一个接口，该接口供 Viewer 或浏览器来获取 Applet 的信息，并且来控制 Applet 的执行。
        </p><p>Viewer 可能是：</p><ul><li>
        请求 Applet 作者、版本和版权的信息</li><li>
        请求 Applet 识别的参数的描述</li><li>
        初始化 Applet</li><li>
        销毁 Applet</li><li>
        开始执行 Applet</li><li>
        结束执行 Applet</li></ul><p>
        Applet 类提供了对这些方法的默认实现，这些方法可以在需要的时候重写。</p><p>
        "Hello，World"applet 都是按标准编写的。唯一被重写的方法是 paint 方法。</p>
        <hr>
        <h2>
            Applet 的调用</h2>
        <p>
            Applet 是一种 Java 程序。它一般运行在支持 Java 的 Web 浏览器内。因为它有完整的 Java API 支持,所以 Applet 是一个全功能的 Java 应用程序。
        </p>
        <p>
            &lt;applet&gt; 标签是在HTML文件中嵌入 Applet 的基础。以下是一个调用"Hello World"applet的例子；
        </p>

        <div class="example">
            <h2 class="example">HTML 代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">html</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;</span><span class="hl-code">The Hello, World Applet</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">hr</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">applet</span><span class="hl-code"> </span><span class="hl-var">code</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">HelloWorldApplet.class</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">width</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">320</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">height</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">120</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">
If your browser was Java-enabled, a "Hello, World"
message would appear here.
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">applet</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">hr</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">html</span><span class="hl-brackets">&gt;</span></pre>
            </div>
        </div>
        <p><strong>
            注意: </strong>你可以参照 HTML Applet 标签来更多的了解从 HTML 中调用 applet 的方法。</p><p>
        &lt;applet&gt; 标签的属性指定了要运行的 Applet 类。width 和 height 用来指定 Applet 运行面板的初始大小。Applet 必须使用 &lt;/applet&gt; 标签来关闭。</p><p>
        如果 Applet 接受参数，那么参数的值需要在 &lt;param&gt; 标签里添加，该标签位于 &lt;applet&gt; 和 &lt;/applet&gt; 之间。浏览器忽略了 applet 标签之间的文本和其他标签。</p><p>
        不支持 Java 的浏览器不能执行 &lt;applet&gt; 和 &lt;/applet&gt;。因此，在标签之间显示并且和 applet 没有关系的任何东西，在不支持的 Java 的浏览器里是可见的。</p><p>
        Viewer 或者浏览器在文档的位置寻找编译过的 Java 代码，要指定文档的路径，得使用 &lt;applet&gt; 标签的 codebase 属性指定。</p><p>
        如下所示：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="tag">&lt;applet</span><span class="pln"> </span><span class="atn">codebase</span><span class="pun">=</span><span class="atv">"http://amrood.com/applets"</span><span class="pln">
</span><span class="atn">code</span><span class="pun">=</span><span class="atv">"HelloWorldApplet.class"</span><span class="pln"> </span><span class="atn">width</span><span class="pun">=</span><span class="atv">"320"</span><span class="pln"> </span><span class="atn">height</span><span class="pun">=</span><span class="atv">"120"</span><span class="tag">&gt;</span></pre>
        <p>
            如果 Applet 所在一个包中而不是默认包，那么所在的包必须在 code 属性里指定，例如：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="tag">&lt;applet</span><span class="pln"> </span><span class="atn">code</span><span class="pun">=</span><span class="atv">"mypackage.subpackage.TestApplet.class"</span><span class="pln">
           </span><span class="atn">width</span><span class="pun">=</span><span class="atv">"320"</span><span class="pln"> </span><span class="atn">height</span><span class="pun">=</span><span class="atv">"120"</span><span class="tag">&gt;</span></pre>
        <h2></h2><h2>获得applet参数</h2>
        <p>

            下面的例子演示了如何使用一个 Applet 响应来设置文件中指定的参数。该 Applet 显示了一个黑色棋盘图案和第二种颜色。</p><p>
        第二种颜色和每一列的大小通过文档中的 Applet 的参数指定。</p><p>
        CheckerApplet 在 init() 方法里得到它的参数。也可以在 paint() 方法里得到它的参数。然而，在 Applet 开始得到值并保存了设置，而不是每一次刷新的时候都得到值，这样是很方便，并且高效的。
    </p><p>Applet viewer 或者浏览器在 Applet 每次运行的时候调用 init() 方法。在加载 Applet 之后，Viewer 立即调用 init() 方法（Applet.init()什么也没做），重写该方法的默认实现，添加一些自定义的初始化代码。
    </p><p>Applet.getParameter() 方法通过给出参数名称得到参数值。如果得到的值是数字或者其他非字符数据，那么必须解析为字符串类型。
    </p><p>下例是 CheckerApplet.java 的修改：</p>
        <div class="example">
            <h2 class="example">CheckerApplet.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">applet</span><span class="hl-code">.*;
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">awt</span><span class="hl-code">.*;
</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">CheckerApplet</span><span class="hl-code"> </span><span class="hl-reserved">extends</span><span class="hl-code"> </span><span class="hl-identifier">Applet</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">squareSize</span><span class="hl-code"> = </span><span class="hl-number">50</span><span class="hl-code">;</span><span class="hl-comment">//</span><span class="hl-comment"> 初始化默认大小</span><span class="hl-comment"></span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">init</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">parseSquareSize</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">param</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-identifier">Color</span><span class="hl-code"> </span><span class="hl-identifier">parseColor</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">param</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">paint</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">Graphics</span><span class="hl-code"> </span><span class="hl-identifier">g</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>
            下面是 CheckerApplet 类的 init() 方法和私有的 parseSquareSize() 方法：</p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">init</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">squareSizeParam</span><span class="hl-code"> = </span><span class="hl-identifier">getParameter</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">squareSize</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-identifier">parseSquareSize</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">squareSizeParam</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">colorParam</span><span class="hl-code"> = </span><span class="hl-identifier">getParameter</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">color</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-identifier">Color</span><span class="hl-code"> </span><span class="hl-identifier">fg</span><span class="hl-code"> = </span><span class="hl-identifier">parseColor</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">colorParam</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-identifier">setBackground</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">Color</span><span class="hl-code">.</span><span class="hl-identifier">black</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-identifier">setForeground</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">fg</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">parseSquareSize</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">param</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">param</span><span class="hl-code"> == </span><span class="hl-reserved">null</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
   </span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">squareSize</span><span class="hl-code"> = </span><span class="hl-identifier">Integer</span><span class="hl-code">.</span><span class="hl-identifier">parseInt</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">param</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">catch</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
     </span><span class="hl-comment">//</span><span class="hl-comment"> 保留默认值</span><span class="hl-comment"></span><span class="hl-code">
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>
            该 Applet 调用 parseSquareSize()，来解析 squareSize 参数。parseSquareSize() 调用了库方法 Integer. parseInt() 该方法将一个字符串解析为一个整数，当参数无效的时候，Integer.parseInt() 抛出异常。
        </p><p>因此，parseSquareSize() 方法也是捕获异常的，并不允许 Applet 接受无效的输入。
    </p><p>Applet 调用 parseColor()方法将颜色参数解析为一个 Color 值。parseColor() 方法做了一系列字符串的比较，来匹配参数的值和预定义颜色的名字。你需要实现这些方法来使 Applet 工作。
    </p>
        <hr><h2>
        指定 applet 参数</h2>
        <p>
            如下的例子是一个HTML文件，其中嵌入了 CheckerApplet 类。HTML文件通过使用 &lt;param&gt; 标签的方法给 applet 指定了两个参数。
        </p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">html</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;</span><span class="hl-code">Checkerboard Applet</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">hr</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">applet</span><span class="hl-code"> </span><span class="hl-var">code</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">CheckerApplet.class</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">width</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">480</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">height</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">320</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">param</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">color</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">value</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">blue</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">param</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">squaresize</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">value</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">30</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">applet</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">hr</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">html</span><span class="hl-brackets">&gt;</span></pre>
            </div>
        </div>
        <p><strong>
            注意: </strong>参数名字大小写不敏感。</p>
        <hr>
        <h2>应用程序转换成 Applet
        </h2><p>
        将图形化的 Java 应用程序（是指，使用AWT的应用程序和使用 java 程序启动器启动的程序）转换成嵌入在web页面里的applet是很简单的。</p><p>
        下面是将应用程序转换成 Applet 的几个步骤：</p>
        <ul><li>
            编写一个 HTML 页面，该页面带有能加载 applet 代码的标签。</li><li>
            编写一个 JApplet 类的子类，将该类设置为 public。否则，Applet 不能被加载。</li><li>
            消除应用程序的  main()方法。不要为应用程序构造框架窗口，因为你的应用程序要显示在浏览器中。</li><li>
            将应用程序中框架窗口的构造方法里的初始化代码移到 Applet 的 init() 方法中，你不必显示的构造 Applet 对象，浏览器将通过调用 init() 方法来实例化一个对象。</li><li>
            移除对 setSize() 方法的调用，对于 Applet 来讲，大小已经通过 HTML 文件里的 width 和 height 参数设定好了。</li><li>
            移除对 setDefaultCloseOperation() 方法的调用。Applet 不能被关闭，它随着浏览器的退出而终止。</li><li>
            如果应用程序调用了 setTitle() 方法，消除对该方法的调用。applet 不能有标题栏。（当然你可以给通过 html 的 title 标签给网页自身命名）</li><li>
            不要调用 setVisible(true),Applet 是自动显示的。</li></ul>
        <hr>
        <h2>
            事件处理</h2>
        <p>

            Applet 类从 Container 类继承了许多事件处理方法。Container 类定义了几个方法，例如：processKeyEvent() 和processMouseEvent()，用来处理特别类型的事件，还有一个捕获所有事件的方法叫做 processEvent。
        </p><p>为了响应一个事件，Applet 必须重写合适的事件处理方法。</p>
        <div class="example">
            <h2 class="example">ExampleEventHandling.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">awt</span><span class="hl-code">.</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">MouseListener</span><span class="hl-code">;
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">awt</span><span class="hl-code">.</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">MouseEvent</span><span class="hl-code">;
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">applet</span><span class="hl-code">.</span><span class="hl-identifier">Applet</span><span class="hl-code">;
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">awt</span><span class="hl-code">.</span><span class="hl-identifier">Graphics</span><span class="hl-code">;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">ExampleEventHandling</span><span class="hl-code"> </span><span class="hl-reserved">extends</span><span class="hl-code"> </span><span class="hl-identifier">Applet</span><span class="hl-code">
                             </span><span class="hl-reserved">implements</span><span class="hl-code"> </span><span class="hl-identifier">MouseListener</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">

    </span><span class="hl-identifier">StringBuffer</span><span class="hl-code"> </span><span class="hl-identifier">strBuffer</span><span class="hl-code">;

    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">init</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-identifier">addMouseListener</span><span class="hl-brackets">(</span><span class="hl-reserved">this</span><span class="hl-brackets">)</span><span class="hl-code">;
         </span><span class="hl-identifier">strBuffer</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">StringBuffer</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">addItem</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">initializing the applet </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">

    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">start</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">addItem</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">starting the applet </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">

    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">stop</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">addItem</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">stopping the applet </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">

    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">destroy</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">addItem</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">unloading the applet</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">

    </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">addItem</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">word</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">word</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">strBuffer</span><span class="hl-code">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-identifier">word</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">repaint</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">

    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">paint</span><span class="hl-brackets">(</span><span class="hl-identifier">Graphics</span><span class="hl-code"> </span><span class="hl-identifier">g</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-comment">//</span><span class="hl-comment">Draw a Rectangle around the applet's display area.</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">g</span><span class="hl-code">.</span><span class="hl-identifier">drawRect</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">,
                      </span><span class="hl-identifier">getWidth</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> - </span><span class="hl-number">1</span><span class="hl-code">,
                      </span><span class="hl-identifier">getHeight</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> - </span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code">;

         </span><span class="hl-comment">//</span><span class="hl-comment">display the string inside the rectangle.</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">g</span><span class="hl-code">.</span><span class="hl-identifier">drawString</span><span class="hl-brackets">(</span><span class="hl-identifier">strBuffer</span><span class="hl-code">.</span><span class="hl-identifier">toString</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">20</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">


    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">mouseEntered</span><span class="hl-brackets">(</span><span class="hl-identifier">MouseEvent</span><span class="hl-code"> </span><span class="hl-identifier">event</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">mouseExited</span><span class="hl-brackets">(</span><span class="hl-identifier">MouseEvent</span><span class="hl-code"> </span><span class="hl-identifier">event</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">mousePressed</span><span class="hl-brackets">(</span><span class="hl-identifier">MouseEvent</span><span class="hl-code"> </span><span class="hl-identifier">event</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">mouseReleased</span><span class="hl-brackets">(</span><span class="hl-identifier">MouseEvent</span><span class="hl-code"> </span><span class="hl-identifier">event</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-code">

    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">mouseClicked</span><span class="hl-brackets">(</span><span class="hl-identifier">MouseEvent</span><span class="hl-code"> </span><span class="hl-identifier">event</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-identifier">addItem</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">mouse clicked! </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>
            如下调用该 Applet：</p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">html</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;</span><span class="hl-code">Event Handling</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">hr</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">applet</span><span class="hl-code"> </span><span class="hl-var">code</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">ExampleEventHandling.class</span><span class="hl-quotes">"</span><span class="hl-code">
</span><span class="hl-var">width</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">300</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">height</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">300</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">applet</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">hr</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">html</span><span class="hl-brackets">&gt;</span></pre>
            </div>
        </div>
        <p>
            最开始运行，Applet 显示 "initializing the applet. Starting the applet."，然后你一点击矩形框，就会显示 "mouse clicked" 。
        </p>
        <hr><h2>显示图片</h2>
        <p>
            Applet 能显示 GIF,JPEG,BMP 等其他格式的图片。为了在 Applet 中显示图片，你需要使用 java.awt.Graphics 类的drawImage()方法。
        </p><p>
        如下实例演示了显示图片的所有步骤：</p>
        <div class="example">
            <h2 class="example">ImageDemo.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">applet</span><span class="hl-code">.*;
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">awt</span><span class="hl-code">.*;
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">net</span><span class="hl-code">.*;
</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">ImageDemo</span><span class="hl-code"> </span><span class="hl-reserved">extends</span><span class="hl-code"> </span><span class="hl-identifier">Applet</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-identifier">Image</span><span class="hl-code"> </span><span class="hl-identifier">image</span><span class="hl-code">;
  </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-identifier">AppletContext</span><span class="hl-code"> </span><span class="hl-identifier">context</span><span class="hl-code">;
  </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">init</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
  </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">context</span><span class="hl-code"> = </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">getAppletContext</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">imageURL</span><span class="hl-code"> = </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">getParameter</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">image</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-identifier">imageURL</span><span class="hl-code"> == </span><span class="hl-reserved">null</span><span class="hl-brackets">)</span><span class="hl-code">
      </span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-identifier">imageURL</span><span class="hl-code"> = </span><span class="hl-quotes">"</span><span class="hl-string">java.jpg</span><span class="hl-quotes">"</span><span class="hl-code">;
      </span><span class="hl-brackets">}</span><span class="hl-code">
      </span><span class="hl-reserved">try</span><span class="hl-code">
      </span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-identifier">URL</span><span class="hl-code"> </span><span class="hl-identifier">url</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">URL</span><span class="hl-brackets">(</span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">getDocumentBase</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-identifier">imageURL</span><span class="hl-brackets">)</span><span class="hl-code">;
         </span><span class="hl-identifier">image</span><span class="hl-code"> = </span><span class="hl-identifier">context</span><span class="hl-code">.</span><span class="hl-identifier">getImage</span><span class="hl-brackets">(</span><span class="hl-identifier">url</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-brackets">}</span><span class="hl-reserved">catch</span><span class="hl-brackets">(</span><span class="hl-identifier">MalformedURLException</span><span class="hl-code"> </span><span class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-code">
      </span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">printStackTrace</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
         </span><span class="hl-comment">//</span><span class="hl-comment"> Display in browser status bar</span><span class="hl-comment"></span><span class="hl-code">
         </span><span class="hl-identifier">context</span><span class="hl-code">.</span><span class="hl-identifier">showStatus</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Could not load image!</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">paint</span><span class="hl-brackets">(</span><span class="hl-identifier">Graphics</span><span class="hl-code"> </span><span class="hl-identifier">g</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">context</span><span class="hl-code">.</span><span class="hl-identifier">showStatus</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Displaying image</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">g</span><span class="hl-code">.</span><span class="hl-identifier">drawImage</span><span class="hl-brackets">(</span><span class="hl-identifier">image</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">84</span><span class="hl-code">, </span><span class="hl-reserved">null</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">g</span><span class="hl-code">.</span><span class="hl-identifier">drawString</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">www.javalicense.com</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-number">35</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div><p>
        如下调用该applet：</p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">html</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;</span><span class="hl-code">The ImageDemo applet</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">hr</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">applet</span><span class="hl-code"> </span><span class="hl-var">code</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">ImageDemo.class</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">width</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">300</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">height</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">200</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">param</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">image</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">value</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">java.jpg</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">applet</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">hr</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">html</span><span class="hl-brackets">&gt;</span></pre>
            </div>
        </div>
        <hr><h2>播放音频</h2>
        <p>

            Applet 能通过使用 java.applet 包中的 AudioClip 接口播放音频。AudioClip 接口定义了三个方法：</p>
        <ul><li><strong>
            public void play():</strong> 从一开始播放音频片段一次。</li><li><strong>
            public void loop(): </strong> 循环播放音频片段</li><li><strong>
            public void stop(): </strong> 停止播放音频片段</li></ul>
        <p>
            为了得到 AudioClip 对象，你必须调用 Applet 类的 getAudioClip() 方法。无论 URL 指向的是否是一个真实的音频文件，该方法都会立即返回结果。
        </p><p>直到要播放音频文件时，该文件才会下载下来。</p><p>
        如下实例演示了播放音频的所有步骤：</p>
        <div class="example">
            <h2 class="example">AudioDemo.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">applet</span><span class="hl-code">.*;
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">awt</span><span class="hl-code">.*;
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">net</span><span class="hl-code">.*;
</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">AudioDemo</span><span class="hl-code"> </span><span class="hl-reserved">extends</span><span class="hl-code"> </span><span class="hl-identifier">Applet</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-identifier">AudioClip</span><span class="hl-code"> </span><span class="hl-identifier">clip</span><span class="hl-code">;
   </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-identifier">AppletContext</span><span class="hl-code"> </span><span class="hl-identifier">context</span><span class="hl-code">;
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">init</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">context</span><span class="hl-code"> = </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">getAppletContext</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">audioURL</span><span class="hl-code"> = </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">getParameter</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">audio</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-identifier">audioURL</span><span class="hl-code"> == </span><span class="hl-reserved">null</span><span class="hl-brackets">)</span><span class="hl-code">
      </span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-identifier">audioURL</span><span class="hl-code"> = </span><span class="hl-quotes">"</span><span class="hl-string">default.au</span><span class="hl-quotes">"</span><span class="hl-code">;
      </span><span class="hl-brackets">}</span><span class="hl-code">
      </span><span class="hl-reserved">try</span><span class="hl-code">
      </span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-identifier">URL</span><span class="hl-code"> </span><span class="hl-identifier">url</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">URL</span><span class="hl-brackets">(</span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">getDocumentBase</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-identifier">audioURL</span><span class="hl-brackets">)</span><span class="hl-code">;
         </span><span class="hl-identifier">clip</span><span class="hl-code"> = </span><span class="hl-identifier">context</span><span class="hl-code">.</span><span class="hl-identifier">getAudioClip</span><span class="hl-brackets">(</span><span class="hl-identifier">url</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-brackets">}</span><span class="hl-reserved">catch</span><span class="hl-brackets">(</span><span class="hl-identifier">MalformedURLException</span><span class="hl-code"> </span><span class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-code">
      </span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">printStackTrace</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
         </span><span class="hl-identifier">context</span><span class="hl-code">.</span><span class="hl-identifier">showStatus</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Could not load audio file!</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">start</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-identifier">clip</span><span class="hl-code"> != </span><span class="hl-reserved">null</span><span class="hl-brackets">)</span><span class="hl-code">
      </span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-identifier">clip</span><span class="hl-code">.</span><span class="hl-identifier">loop</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">stop</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-identifier">clip</span><span class="hl-code"> != </span><span class="hl-reserved">null</span><span class="hl-brackets">)</span><span class="hl-code">
      </span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-identifier">clip</span><span class="hl-code">.</span><span class="hl-identifier">stop</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>
            如下调用applet：</p>
        <div class="example">
            <div class="example_code">
                <div class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">html</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;</span><span class="hl-code">The ImageDemo applet</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">hr</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">applet</span><span class="hl-code"> </span><span class="hl-var">code</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">ImageDemo.class</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">width</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">0</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">height</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">0</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">param</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">audio</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">value</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">test.wav</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">applet</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">hr</span><span class="hl-brackets">&gt;</span></div>
            </div>
        </div>
        <p>
            你可以使用你电脑上的 test.wav 来测试上面的实例。
        </p>

    </div>
</div>

</body>
</html>
